﻿using Furion.DatabaseAccessor;
using Furion.DependencyInjection;
using Furion.FriendlyException;
using HiSport.Application.System.User.Service;
using HiSport.Core;
using HiSport.Core.Const;
using HiSport.Core.Entity;
using HiSport.Core.Enum;
using HiSport.SqlSugar;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HiSport.Application.System.Menu
{
    public class SysMenuService : ITransient, ISysMenuService
    {
        private readonly SqlSugarRepository<SysMenu> _sysMenuRep;
        private readonly SysCacheService _sysCacheService;
        public SysMenuService(SqlSugarRepository<SysMenu> sysMenuRep,
        SysCacheService sysCacheService)
        {
            
            _sysMenuRep = sysMenuRep;
            
            _sysCacheService = sysCacheService;
        }
        ///// <summary>
        ///// 增加菜单
        ///// </summary>
        ///// <param name="input"></param>
        ///// <returns></returns>
        //[ApiDescriptionSettings(Name = "Add"), HttpPost]
        //[DisplayName("增加菜单")]
        //public async Task AddMenu(AddMenuInput input)
        //{
        //    var isExist = input.Type != MenuTypeEnum.Btn
        //        ? await _sysMenuRep.IsAnyAsync(u => u.Title == input.Title)
        //        : await _sysMenuRep.IsAnyAsync(u => u.Permission == input.Permission);

        //    if (isExist)
        //        throw Oops.Oh(ErrorCodeEnum.D4000);

        //    // 校验菜单参数
        //    var sysMenu = input.Adapt<SysMenu>();
        //    CheckMenuParam(sysMenu);

        //    await _sysMenuRep.InsertAsync(sysMenu);

        //    // 清除缓存
        //    DeleteMenuCache();
        //}

        ///// <summary>
        ///// 更新菜单
        ///// </summary>
        ///// <param name="input"></param>
        ///// <returns></returns>
        //[ApiDescriptionSettings(Name = "Update"), HttpPost]
        //[DisplayName("更新菜单")]
        //public async Task UpdateMenu(UpdateMenuInput input)
        //{
        //    if (input.Id == input.Pid)
        //        throw Oops.Oh(ErrorCodeEnum.D4008);

        //    var isExist = input.Type != MenuTypeEnum.Btn
        //        ? await _sysMenuRep.IsAnyAsync(u => u.Title == input.Title && u.Type == input.Type && u.Id != input.Id)
        //        : await _sysMenuRep.IsAnyAsync(u => u.Permission == input.Permission && u.Type == input.Type && u.Id != input.Id);
        //    if (isExist)
        //        throw Oops.Oh(ErrorCodeEnum.D4000);

        //    // 校验菜单参数
        //    var sysMenu = input.Adapt<SysMenu>();
        //    CheckMenuParam(sysMenu);

        //    await _sysMenuRep.AsUpdateable(sysMenu).ExecuteCommandAsync();

        //    // 清除缓存
        //    DeleteMenuCache();
        //}

        ///// <summary>
        ///// 删除菜单
        ///// </summary>
        ///// <param name="input"></param>
        ///// <returns></returns>
        //[UnitOfWork]
        //[ApiDescriptionSettings(Name = "Delete"), HttpPost]
        //[DisplayName("删除菜单")]
        //public async Task DeleteMenu(DeleteMenuInput input)
        //{
        //    var menuTreeList = await _sysMenuRep.AsQueryable().ToChildListAsync(u => u.Pid, input.Id, true);
        //    var menuIdList = menuTreeList.Select(u => u.Id).ToList();

        //    await _sysMenuRep.DeleteAsync(u => menuIdList.Contains(u.Id));

        //    // 级联删除角色菜单数据
        //    await _sysRoleMenuService.DeleteRoleMenuByMenuIdList(menuIdList);

        //    // 清除缓存
        //    DeleteMenuCache();
        //}

        ///// <summary>
        ///// 增加和编辑时检查菜单数据
        ///// </summary>
        ///// <param name="menu"></param>
        //private static void CheckMenuParam(SysMenu menu)
        //{
        //    var permission = menu.Permission;
        //    if (menu.Type == MenuTypeEnum.Btn)
        //    {
        //        menu.Name = null;
        //        menu.Path = null;
        //        menu.Component = null;
        //        menu.Icon = null;
        //        menu.Redirect = null;
        //        menu.OutLink = null;
        //        menu.IsHide = false;
        //        menu.IsKeepAlive = true;
        //        menu.IsAffix = false;
        //        menu.IsIframe = false;

        //        if (string.IsNullOrEmpty(permission))
        //            throw Oops.Oh(ErrorCodeEnum.D4003);
        //        if (!permission.Contains(':'))
        //            throw Oops.Oh(ErrorCodeEnum.D4004);
        //    }
        //    else
        //    {
        //        menu.Permission = null;
        //    }
        //}

        /// <summary>
        /// 清除菜单和按钮缓存
        /// </summary>
        private void DeleteMenuCache()
        {
            _sysCacheService.RemoveByPrefixKey(CacheConst.KeyUserMenu);
            _sysCacheService.RemoveByPrefixKey(CacheConst.KeyUserButton);
        }
    }
}
